const userModel = require('../lib/mysql.js')
const md5 = require('md5')
const jwt = require('jsonwebtoken');
const {setToken} = require('../middlewares/token_verify')


// exports.getSignin = async ctx => {
//     await checkNotLogin(ctx)
//     await ctx.render('signin', {
//         session: ctx.session,
//     })
// }
exports.postSignin = async ctx => {
    console.log(ctx.request)
    let {
        name,
        password
    } = ctx.request.body
    await userModel.findDataByName(name)
        .then(result => {
            let res = result
            console.log(res)
            if (res.length && name === res[0]['name'] && md5(password) === res[0]['pass']) {
                setToken(res[0]['name'], res[0]['id']).then(token => {
                    ctx.body = {
                        code: 200,
                        message: '登录成功',
                        userInfo: {
                            user: res[0]['name'],
                            id: res[0]['id'],
                            avatar: res[0]['avator'],
                            token:token
                        }
                    }
                })
                
            } else {
                ctx.body = {
                    code: 500,
                    message: '用户名或密码错误'
                }
                console.log('用户名或密码错误!')
            }
        }).catch(err => {
            console.log(err)
        })
}